#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef struct _IO_struct_s {
	string name;
	string Sum;
} IOStruct_t;

typedef struct _DataStruct_s {
	string functionName;
	string functionSum;
	string functionDetail;
	
	int inputSize;
	IOStruct_t* inputItem;
	
	int outputSize;
	IOStruct_t* outputItem;
	
	string warnningStr;
	
	string history;
	
}DataStruct_t;

string nstr[1024*10];
int offset;
DataStruct_t my_data;

void user_data_init(DataStruct_t *data)
{
	if (!data)
		return ;
	cout << "function name:" <<endl;
	getline(cin, data->functionName);
	cout << "function Sum:" <<endl;
	getline(cin, data->functionSum);
	cout << "function detail:" <<endl;
	getline(cin, data->functionDetail);
	cout << "input size:" <<endl;
	cin >> data->inputSize;
	cout << "output size:" <<endl;
	cin >> data->outputSize;
	cin.clear();  // 清除错误标志   
    cin.ignore(1); //清除缓冲区的当前行
	data->inputItem = new IOStruct_t[data->inputSize];
	data->outputItem = new IOStruct_t[data->outputSize];
	if (!data->inputItem || !data->outputItem)
		return ;
	for (int i = 0; i < data->inputSize; i++) {
		cout << "input a input item name:" ;
		getline(cin, data->inputItem[i].name) ;
		cout << data->inputItem[i].name << endl;
		cout << "input a input item sum:" ;
		getline(cin, data->inputItem[i].Sum);
		cout << data->inputItem[i].name << endl;
	}
	for (int i = 0; i < data->outputSize; i++) {
		cout << "input a output item name:" ;
		getline(cin, data->outputItem[i].name) ;
		cout << "input a output item sum:" ;
		getline(cin, data->outputItem[i].Sum);
	}
	cout << "warnning:" << endl;
	cin >> data->warnningStr;
	
	cout << "history:" << endl;
	cin >> data->history;
}

void null_line(int n)
{
	while (n--) {
		cout << " *" ;cout.width(offset - 2);cout << cout.fill(' ') << "*" << endl;
	}
}
void do_function_section(const char *fname, const char *fsum, const char *fdetail)
{
	//  The function section
	string tmp;
	int i;
    cout << " * " ;
	tmp = string((fname ? fname : "none")) + string(" -- ") + string((fsum ? fsum : "none")) ;
	cout << tmp  ;
	cout.width(offset-tmp.size() - 2);
	cout << cout.fill(' ') << "*" ;
	cout <<endl;
	null_line(1);
	for (i = 0, tmp = string(fdetail); i < tmp.size(); i += (offset-5)) {
		cout << " *" ;
		cout.width(4);
		cout << cout.fill(' ') << tmp.substr(i, (offset-5));
		if ((i+offset-5) > tmp.size())
			cout.width(tmp.substr(i, (offset-5)).size());
		else 
			cout.width(5);
		cout << cout.fill(' ') << "*" ;
		cout << endl;
	}
	null_line(1);
}

void do_input_section(const char *name, const char *detail)
{
	string tmp;
	tmp += string(" * INPUT:        ");
	tmp += string(name);
	tmp += string(" -- ");
	tmp += string(detail);
	cout << tmp ;
	if ((offset-tmp.size()))
		cout.width((offset-tmp.size()));
	cout << cout.fill(' ') << "*";
	cout << endl;
	null_line(1);
}

int main(int argc, const char *argv[])
{
	int i;
	user_data_init(&my_data);
    offset = atoi(argv[1]);
	offset = string(my_data.functionName + my_data.functionSum).size() + 12;
    // output first line
    cout << "/*";//cout.setf(ios::right);
    cout.width(offset);
    cout << cout.fill('*');
    cout << endl;
    //cout.unsetf(ios::right);
	
	do_function_section(my_data.functionName.c_str(), 
		my_data.functionSum.c_str(), my_data.functionDetail.c_str());
#if 0
    //  The function section
    cout << " *" ;
    cout.width(offset-2);
    //  more code
    cout << cout.fill(' ') << "*" ;
    cout << endl;
#endif
	i = 0;
	while (i < my_data.inputSize) {
		do_input_section(my_data.inputItem[i].name.c_str(), my_data.inputItem[i].Sum.c_str());
		i++;
	}
#if 0
    //  The Input section
    cout << " *" ;
    cout.width(offset-2);
    cout << cout.fill(' ') << "*" ;
    //  Every item describer
    cout << endl;
#endif
    //  The Output section
    cout << " *" ;
    cout.width(offset-2);
    cout << cout.fill(' ') << "*" ;
    //  Every item
    cout << endl;

    //  The Warnning secion
    cout << " *" ;
    cout.width(offset-2);
    cout << cout.fill(' ') << "*" ;
    //  Detail text
    cout << endl;

    // output last line
    cout << " *" ;
    cout.width(offset-1);
    cout << cout.fill('=');
    cout << "*/" << endl;
    return 0;
}
